/**
 * Copyright &copy; 2016-2022 liuhangjun All rights reserved.
 */
package com.channelsharing.hongqu.portal.api.service.impl;

import com.channelsharing.common.entity.Paging;
import com.channelsharing.common.service.CrudServiceImpl;
import com.channelsharing.hongqu.portal.api.constant.Constant;
import com.channelsharing.hongqu.portal.api.dao.GroupBuyDao;
import com.channelsharing.hongqu.portal.api.entity.GroupBuyProduct;
import com.channelsharing.hongqu.portal.api.entity.ProductInfo;
import com.channelsharing.hongqu.portal.api.service.GroupBuyService;
import com.channelsharing.hongqu.portal.api.service.ProductInfoService;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 团购Service
 * @author liuhangjun
 * @version 2018-06-22
 */
@Service
public class GroupBuyServiceImpl extends CrudServiceImpl<GroupBuyDao, GroupBuyProduct> implements GroupBuyService {

    @Autowired
    private ProductInfoService productInfoService;

    public static final String PORTAL_CACHE_PREFIX = Constant.PORTAL_CACHE_PREFIX;

    @Override
    public GroupBuyProduct findOne(Long id) {
        GroupBuyProduct groupBuyProduct = new GroupBuyProduct();
        groupBuyProduct.setId(id);
        ProductInfo productInfo = productInfoService.findOne(groupBuyProduct.getProductId());
        groupBuyProduct.setProductInfo(productInfo);
        return super.findOne(groupBuyProduct);
    }

    @Override
    public Paging<GroupBuyProduct> findPaging(@NonNull GroupBuyProduct entity) {
        Paging<GroupBuyProduct> paging = super.findPaging(entity);
        List<GroupBuyProduct> rows = paging.getRows();
        if(!rows.isEmpty()){
            rows.forEach(groupBuyProduct -> {
                ProductInfo productInfo = productInfoService.findOne(groupBuyProduct.getProductId());
                groupBuyProduct.setProductInfo(productInfo);
            });
        }

        return paging;
    }
}
